package cuit.blog.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


import cuit.blog.dao.ReplyRespository;
import cuit.blog.entity.Article;
import cuit.blog.entity.Reply;


import jakarta.transaction.Transactional;

@Service
@Transactional
public class ReplyService {
    @Autowired
    ReplyRespository ReplyRespository;
    
    /**
     * 查找所有
     * @return
     */
    public List<Reply> findByArticle_id(Long article_id){
    	return ReplyRespository.findByArticle_id(article_id);
    }
    /**
     * 查找所有
     * @return
     */
    public List<Reply> findByUser_id(Long User_id){
    	return ReplyRespository.findByUser_id(User_id);
    }
    /**
     * 保存
     * 
     * @param reply
     * @return
     */
	public Reply save(Reply reply) {
		// TODO Auto-generated method stub
		return ReplyRespository.save(reply);
	}
	
	/**
     * 分页查找
     * 
     * @return
     */
	public Page<Reply> findAllByPage(int pageIndex, int pageSize) {
		//排序
		Sort sort = Sort.by(Sort.Direction.ASC, "id");
		//分页
		Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
		//分页查询
		Page<Reply> replyPage = ReplyRespository.findAll(pageable);
		return replyPage;
	}
	/**
     * 按user_id分页查找
     * 
     * @return
     */
	public Page<Reply> getRepliesByUser(Long userId, int pageIndex, int pageSize) {
        Pageable pageable = PageRequest.of(pageIndex, pageSize,Sort.by(Sort.Direction.ASC, "id"));
        return ReplyRespository.findByUserId(userId, pageable);
    }
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		ReplyRespository.deleteById(id);
	}
}
